package 算法.leetcode.labuladong.数组和链表;

import java.util.Stack;

/**
 * @author lchenglong
 * @date 2022/6/17
 */
public class 有效的括号 {
    static class Solution {

        public static void main(String[] args) {
            Solution solution = new Solution();
            System.out.println(solution.isValid("()"));
        }
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            for (Character c: s.toCharArray()){
                if (c == '('||c == '['||c=='{') {
                    stack.push(c);
                } else if (!stack.empty()&&stack.peek().equals(leftOf(c))){
                    stack.pop();
                } else {
                    return false;
                }
            }
            return stack.empty();
        }

        public Character leftOf(Character c){
            if (c == '}')return '{';
            else if (c == ')')return '(';
            else return '[';
        }
    }
}
